<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-5-28
 * Time: 上午11:17
 */

namespace api\modules\chedai\controllers;


use common\components\tools\Tools;
use common\components\wodrow\umeng\MessageClient;
use common\models\credit_app\Member;
use common\models\credit_app\Message;
use console\models\app\OrderCert;
use wodrow\yii\rest\Controller;
use yii\base\ErrorException;

class NotifyController extends Controller
{
    /**
     * 测试
     * @desc 各种消息及推送测试
     */
    public function actionTest()
    {
        $data = [
            'type' => MessageClient::TYPE_CUSTOMIZEDCAST,
            'alias_type' => 'user',
            'alias' => [95, 1255],
            'payload' => [
                'display_type' => MessageClient::DISPLAY_TYPE_NOTIFICATION,
                'body' => [
                    'title' => 'test',
                    'text' => '测试',
                ],
            ],
        ];
        $r = \Yii::$app->umeng->send($data);
        $r= json_decode($r, true);
        if (isset($r['data']['json'])&&$r['data']['json']){
            $r['data']['json_arr'] = json_decode($r['data']['json']);
        }
        return $r;
    }

    /**
     * 实时面签结果通知
     * @desc get/post
     * @param string $busi_id "!ysjr".str_pad($ct_id, 12, '0', STR_PAD_LEFT);
     * @param int $bool 状态 0/1
     * @param string $remark 备注
     * @return array
     * @throws ErrorException
     */
    public function actionSendCtNotify($busi_id, $bool = 1, $remark = '', $serv_pw)
    {
        $a = str_replace("!ysjr", '', $busi_id);
        $ct_id = (int)$a;
        $x = Tools::str2arr($serv_pw, '_');
        if ($x[1]!='asdgasdfasd554'){
            throw new ErrorException('服务器访问码错误');
        }
        $cert = OrderCert::findOne(['ct_id'=>$ct_id]);
        if (!$cert){
            throw new ErrorException("没有找到征信");
        }
        $order = $cert->order;
        if (!$order){
            throw new ErrorException("没有找到订单");
        }
        $m_uid = $order->o_uid;
        if ($bool){
            $msg="审核结果:通过 ";
        }else{
            $msg="审核结果:不通过 ";
        }
        if ($remark){
            $msg .= "审核备注:".$remark." ";
        }
        $title = '实时面签结果';
        $text = "客户:".$cert->ct_name." ".$msg;
        $data = [
            'type' => MessageClient::TYPE_CUSTOMIZEDCAST,
            'alias_type' => 'user',
            'alias' => [$m_uid],
            'payload' => [
                'display_type' => MessageClient::DISPLAY_TYPE_NOTIFICATION,
                'body' => [
                    'title' => $title,
                    'text' => $text,
                ],
            ],
        ];
        $r = \Yii::$app->umeng->send($data);
        $r= json_decode($r, true);
        if (isset($r['ret'])&&$r['ret']=='SUCCESS'){}else{
            throw new ErrorException("push失败");
        }
        if (isset($r['data']['json'])&&$r['data']['json']){
            $r['data']['json_arr'] = json_decode($r['data']['json']);
        }
        $message = new Message();
//        $message->m_from = 0;
        $message->m_uid = $m_uid;
        $message->m_title = $title;
        $message->m_name = $cert->ct_name;
        $message->m_content = $text;
        $message->m_dateline = time();
        $message->m_oid = $order->o_id;
        $message->m_info = serialize(['status'=>"实时面签", 'code'=>15]);
//        $message->m_status = 0;
        if (!$message->save()){
            throw new ErrorException("message保存失败".var_export($message->errors, true));
        }
        return $r;
    }

    /**
     * 评估结果通知
     * @desc get/post
     * @param string $m_uid 通知用户
     * @param string $text 通知内容
     * @param int $bool 状态 0/1
     * @param string $remark 备注
     * @return array
     * @throws ErrorException
     */
    public function actionSendEstimateNotify($m_uid, $text, $bool = 1, $remark = '', $serv_pw)
    {
        $x = Tools::str2arr($serv_pw, '_');
        if ($x[1]!='asdgasdfasd554'){
            throw new ErrorException('服务器访问码错误');
        }
        $member = Member::findOne(['u_id'=>$m_uid]);
        if (!$member){
            throw new ErrorException("没有找到对应通知用户");
        }
        if ($bool){
            $msg="审核结果:[通过] ";
        }else{
            $msg="审核结果:[退回] ";
        }
        if ($remark){
            $msg .= "审核备注:".$remark." ";
        }
        $title = '评估结果';
        $data = [
            'type' => MessageClient::TYPE_CUSTOMIZEDCAST,
            'alias_type' => 'user',
            'alias' => [$m_uid],
            'payload' => [
                'display_type' => MessageClient::DISPLAY_TYPE_NOTIFICATION,
                'body' => [
                    'title' => $title,
                    'text' => $text,
                ],
            ],
        ];
        $r = \Yii::$app->umeng->send($data);
        $r= json_decode($r, true);
        if (isset($r['ret'])&&$r['ret']=='SUCCESS'){}else{
            throw new ErrorException("push失败");
        }
        if (isset($r['data']['json'])&&$r['data']['json']){
            $r['data']['json_arr'] = json_decode($r['data']['json']);
        }
        $message = new Message();
//        $message->m_from = 0;
        $message->m_uid = $m_uid;
        $message->m_title = $title;
        $message->m_name = $member->u_realname;
        $message->m_content = $msg.$text;
        $message->m_dateline = time();
        $message->m_oid = 0;
        $message->m_info = serialize(['status'=>"评估结果", 'code'=>16]);
//        $message->m_status = 0;
        if (!$message->save()){
            throw new ErrorException("message保存失败".var_export($message->errors, true));
        }
        return $r;
    }
}